#include "cmdacademyboss.h"
#include "../mainctrl.h"
#include "../cmd/dataprepare.h"
#include "commmsg.h"

CT_REGISTER_ASYNCCMD(ID_MAIN_ACADEMY_BOSS, CCmdAcademyBoss);

int CCmdAcademyBoss::Do(void* pData)
{
    CT_ASSERT(pData);

    //解析消息
    CCmdPBDoParam* pstParam = (CCmdPBDoParam*) pData;
    pstMainCtrl = (CMainCtrl*) pstParam->m_pMain;

    if (pstParam->m_pstMsgHead->msgid() != ID_MAIN_ACADEMY_BOSS ||
        pstParam->m_pstMsgHead->msgtype() != CSMsgDef::EMT_REQUEST)
    {
        CT_ERROR(("Cmd Stat Err Cmd=%d CmdID=%d Tpye=%d",
            ID_MAIN_ACADEMY_BOSS,
            pstParam->m_pstMsgHead->msgid(),
            pstParam->m_pstMsgHead->msgtype()));
        return Done(RET_SYS_ERROR);
    }

    stMsgHead = *(pstParam->m_pstMsgHead);
    stEnpHead = *(pstParam->m_pstEnpHead);

    int iRet = pstMainCtrl->DecodePBMsg(stReq, pstParam); //消息解析
    if (iRet)
    {
        return AnsClients(iRet);
    }

	chSubCmd = stReq.sub_type();
    iAcademyType = stReq.academy_type();
    uiGID = stReq.gid();

    //检查登录
    if (CAcademyBossMgr::CMD_ACADEMY_BOSS_SUB_ROLE_INFO != chSubCmd)
    {
        iRet = pstMainCtrl->CheckLogin(uiGID, stEnpHead);
        if (iRet != RET_OK)
        {
            return AnsClients(iRet);
        }
    }

    stAns.set_gid((int)uiGID);
    stAns.set_sub_type(chSubCmd);

    LogicDo(RET_OK);
    return RET_OK;
}

int CCmdAcademyBoss::LogicDo(int iRet)
{
    if (iRet != 0)
    {
        return AnsClients(iRet);
    }

    PREPARE_PLAYER_BASE_INFO(this, uiGID);
    ASSERT_PLAYER_PREPARED(uiGID);

    PREPARE_PLAYER_DETAIL_INFO(this, uiGID);
    ASSERT_PLAYER_DETAIL_PREPARED(uiGID);

    PREPARE_PLAYER_SNS_DETAIL_INFO(this, uiGID);
    ASSERT_PLAYER_SNS_DETAIL_PREPARED(uiGID);

    PREPARE_PLAYER_ONLINE_DATA(this, uiGID);

    int iIndex = pstMainCtrl->m_stPlayerBaseInfoHash.HashFind(uiGID);
    if (iIndex < 0)
	{
        CT_ERROR(("no base data user(%u)", uiGID));
        return AnsClients(RET_SYSTEM_FAIL);
	}
	CPlayerBaseInfo& stPlayerBase = pstMainCtrl->m_stPlayerBaseInfoHash[iIndex];

    if (stPlayerBase._ucIsInited != 1)
    {
        return AnsClients(ERROR_NOT_INITED);
    }

    iIndex = pstMainCtrl->m_stPlayerDetailInfoHash.HashFind(uiGID);
    if (iIndex < 0)
	{
        CT_ERROR(("no detail data user(%u)", uiGID));
        return AnsClients(RET_SYSTEM_FAIL);
	}
	CPlayerDetailInfo& stPlayerDetail
        = pstMainCtrl->m_stPlayerDetailInfoHash[iIndex];

    iIndex = pstMainCtrl->m_stPlayerOnlineDataHash.HashFind(uiGID);
    if (iIndex < 0)
    {
        CT_ERROR(("no online data user(%u)", uiGID));
        return AnsClients(RET_SYSTEM_FAIL);

    }
	CPlayerOnlineData& stPlayerOnline
        = pstMainCtrl->m_stPlayerOnlineDataHash[iIndex];

    iIndex = pstMainCtrl->FindPlayerCacheData(uiGID);
    if (iIndex < 0)
    {
        CT_ERROR(("no cache data user %u", uiGID));
        return AnsClients(RET_SYSTEM_FAIL);
    }
	CPlayerCacheData& stPlayerCache
        = pstMainCtrl->m_stPlayerCacheDataHash[iIndex];

    iIndex = pstMainCtrl->m_stPlayerSnsCommInfoHash.HashFind(uiGID);
    if (iIndex < 0)
    {
        CT_ERROR(("no sns comm data user %u", uiGID));
        return AnsClients(RET_SYSTEM_FAIL);
    }
    CPlayerSnsCommInfo& stPlayerSnsComm =
        pstMainCtrl->m_stPlayerSnsCommInfoHash[iIndex];

    stCmdData.Clear();
	switch (chSubCmd)
	{
	case CAcademyBossMgr::CMD_ACADEMY_BOSS_SUB_BUFF:
        {
    	    iRet = pstMainCtrl->m_stAcademyBossMgr.BuyMerlin(stPlayerBase,
                stPlayerCache, stReq, stAns);
        }
		break;
	case CAcademyBossMgr::CMD_ACADEMY_BOSS_SUB_REVIVE:
        {
    	    iRet = pstMainCtrl->m_stAcademyBossMgr.DeadRevive(stPlayerBase,
                stPlayerCache, stReq, stAns);
        }
		break;
	case CAcademyBossMgr::CMD_ACADEMY_BOSS_SUB_BATTLE:
        {
    	    iRet = pstMainCtrl->m_stAcademyBossMgr.BossBattle(stPlayerBase,
                stPlayerDetail, stPlayerOnline, stPlayerCache, stReq, stAns,
                stCmdData);

            if (RET_OK == iRet)
            {
                //触发任务
                pstMainCtrl->m_stDailyTaskMgr.TriggerTaskFinish(stPlayerOnline._stUserDailyTask, DAILY_TASK_ID_23);
            }
        }
		break;
    case CAcademyBossMgr::CMD_ACADEMY_BOSS_SUB_ATK_RANK:
        {
            iRet = pstMainCtrl->m_stAcademyBossMgr.GetAcademyBossSceneInfo(
                stPlayerCache, stAns, iAcademyType);
        }
        return AnsClients(iRet);
    case CAcademyBossMgr::CMD_ACADEMY_BOSS_SUB_ROLE_INFO:
        {
            iRet = pstMainCtrl->m_stAcademyBossMgr.SetAcademyBossMonRole(
                stPlayerBase, stPlayerDetail, stPlayerOnline, stPlayerSnsComm,
                stReq);
            if (iRet) {CT_ERROR(("set academy(%d)monster(%d)role(%u)error(%d)",
                       stReq.academy_type(),stReq.monster_type(),uiGID,iRet));}
        }
        return Done(iRet);
	default:
		iRet = ERROR_MAIN_SUB_CMD;
		break;
	}

    if (iRet)
    {
        CT_TRACE(("user(gid=%u) deal academy boss op(sub=%d) error(%d)",
                  uiGID,chSubCmd,iRet));
        return AnsClients(iRet);
    }

	if (chSubCmd == CAcademyBossMgr::CMD_ACADEMY_BOSS_SUB_BUFF ||
		chSubCmd == CAcademyBossMgr::CMD_ACADEMY_BOSS_SUB_REVIVE ||
		chSubCmd == CAcademyBossMgr::CMD_ACADEMY_BOSS_SUB_BATTLE)
	{
    	//保存数据 base info
    	CVarObjectKey stKey;
    	stKey.m_chDataType = DT_PLAYER_BASE_INFO;
    	stKey.m_u64ID = uiGID;
    	pstMainCtrl->UpdateData(stKey, stPlayerBase, VOP_UPDATE);

    	//保存数据 detail info
    	stKey.m_chDataType = DT_PLAYER_DETAIL_INFO;
    	stKey.m_u64ID = uiGID;
    	pstMainCtrl->UpdateData(stKey, stPlayerDetail, VOP_UPDATE);
	}

    return AnsClients(RET_OK);
}

int CCmdAcademyBoss::AnsClients(int iRet)
{
    pstMainCtrl->m_stEncodeBuf.Clear();

    stMsgHead.set_msgret(iRet);
    stMsgHead.set_msgtype(CSMsgDef::EMT_ANSWER);

    if (iRet != RET_OK)
    {
        pstMainCtrl->ReturnMsgHead(stMsgHead, stEnpHead);
        return Done(iRet);
    }

    pstMainCtrl->EncodePBMsg(stMsgHead, pstMainCtrl->m_stEncodeBuf);
    pstMainCtrl->EncodePBMsg(stAns, pstMainCtrl->m_stEncodeBuf);

    if(pstMainCtrl->AnswerToClient(stEnpHead,pstMainCtrl->m_stEncodeBuf,true))
    {
        return Done(RET_SYS_ERROR);
    }

    if (stCmdData.GetCloseActID() > 0)
    {
        pstMainCtrl->m_stActMgr.SetActClose(stCmdData.GetCloseActID());
        pstMainCtrl->FlushSendCache();
    }

    MsgTips& stFootboySelfTips = stCmdData.GetFootboySelfTips();
    if (stFootboySelfTips.IsInitialized())
    {
        pstMainCtrl->SendOnlineTips(stFootboySelfTips);
        pstMainCtrl->FlushSendCache();
    }

    MsgTips& stFootboyOtherTips = stCmdData.GetFootboyOtherTips();
    if (stFootboyOtherTips.IsInitialized())
    {
        pstMainCtrl->SendOnlineTips(stFootboyOtherTips);
        pstMainCtrl->FlushSendCache();
    }

    MsgTips& stFiftySelfTips = stCmdData.GetFiftySelfTips();
    if (stFiftySelfTips.IsInitialized())
    {
        pstMainCtrl->SendOnlineTips(stFiftySelfTips);
        pstMainCtrl->FlushSendCache();
    }

    MsgTips& stFiftyOtherTips = stCmdData.GetFiftyOtherTips();
    if (stFiftyOtherTips.IsInitialized())
    {
        pstMainCtrl->SendOnlineTips(stFiftyOtherTips);
        pstMainCtrl->FlushSendCache();
    }

    MsgTips& stKillSelfTips = stCmdData.GetKillSelfTips();
    if (stKillSelfTips.IsInitialized())
    {
        pstMainCtrl->SendOnlineTips(stKillSelfTips);
        pstMainCtrl->FlushSendCache();
    }

    MsgTips& stKillFirstTips = stCmdData.GetKillFirstTips();
    if (stKillFirstTips.IsInitialized())
    {
        pstMainCtrl->SendOnlineTips(stKillFirstTips);
        pstMainCtrl->FlushSendCache();
    }

    MsgTips& stKillOtherTips = stCmdData.GetKillOtherTips();
    if (stKillOtherTips.IsInitialized())
    {
        pstMainCtrl->SendOnlineTips(stKillOtherTips);
        pstMainCtrl->FlushSendCache();
    }

    return Done(iRet);
}

